package com.itboye.eweilisten.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.eweilisten.R;
import com.itboye.eweilisten.app.MyApplication;
import com.itboye.eweilisten.base.BaseFragment;
import com.itboye.eweilisten.me.login.LoginActivity;
import com.itboye.eweilisten.me.personal.activity.ActivityBooks;
import com.itboye.eweilisten.me.personal.activity.SetUpActivity;
import com.itboye.eweilisten.me.personal.activity.WebActivity;
import com.itboye.eweilisten.util.logincontroller.LoginController;
import com.itboye.eweilisten.util.logincontroller.UnLoginState;
import com.itboye.eweilisten.util.sputil.Const;
import com.itboye.eweilisten.util.sputil.SPUtils;
import com.itboye.eweilisten.util.widget.ActionBarClickListener;
import com.itboye.eweilisten.util.widget.FilterUtil;
import com.itboye.eweilisten.util.widget.IsStringSPUtils;
import com.itboye.eweilisten.util.widget.RoundImageView;
import com.itboye.eweilisten.util.widget.TranslucentActionBar;
import com.itboye.eweilisten.util.widget.TranslucentScrollView;
import com.itboye.eweilisten.util.widget.XImageLoader;
import com.umeng.message.UTrack;

/**
 * 我的
 */

public class MeFragment extends BaseFragment implements ActionBarClickListener, TranslucentScrollView.TranslucentChangedListener {
    private TextView my_xinxi, my_zhanghu, my_anquan, my_bangzhu, myBooks, tuijian;
    private Button my_ok;
    private ImageView img_setting;
    private RoundImageView person_img, person_imgtwo;
    private MediaPlayer mediaPlayer;
    private TextView tvNikeNmae;
    private String Nickname;//呢称
    private ImageView close_icon;

//    http://sc1.111ttt.com/2017/1/03/07/296072048390.mp3

    private TranslucentScrollView translucentScrollView;
    private TranslucentActionBar actionBar;
    private View zoomView;
    public static String EXIT = "com.itboye.eweilisten.exit";// 退出登录的广播

    @Override
    public int initView() {
        return R.layout.fragment_me;
    }

    @Override
    public void onMyActivityCreated() {

        init();
        inittwo();
        close_icon.setVisibility(View.GONE);
        Nickname = (String) SPUtils.get(getActivity(), null, Const.NICK, "");
        IntentFilter intentFilter = new IntentFilter(FilterUtil.LOGIN);
        getActivity().registerReceiver(nichengBroadcastReceiver, intentFilter);
        IntentFilter HEADFiele = new IntentFilter(FilterUtil.HEAD);
        getActivity().registerReceiver(nichengBroadcast, HEADFiele);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        getActivity().unregisterReceiver(nichengBroadcast);
        getActivity().unregisterReceiver(nichengBroadcastReceiver);
    }

    public void init() {
        Log.d("uiduidiudui", IsStringSPUtils.IsUid());
        if ((boolean) SPUtils.get(getActivity(), null, Const.IS_LOGINED, false)) {
            String nikeName = (String) SPUtils.get(getActivity(), null,
                    Const.NICK, "");
            String url = (String) SPUtils.get(getActivity(), null,
                    Const.HEAD, "");
            if (nikeName.equals("") || nikeName == null) {
                my_ok.setVisibility(View.GONE);
                tvNikeNmae.setText("请点击头像登录");
            } else {
                my_ok.setVisibility(View.VISIBLE);
                tvNikeNmae.setText("["+ (String) SPUtils.get(getActivity(), null,
                        Const.ID, "")+"]" + nikeName);
            }
            if (url.equals("")|| url == null){
                person_img.setImageResource(R.drawable.logo_one);
            }else{
                XImageLoader.load(Const.IMAGE_HEAD + url,
                        person_img);
            }
        } else {
            tvNikeNmae.setText("请点击头像登录");
            person_img.setImageResource(R.drawable.logo_one);
        }
    }

    BroadcastReceiver nichengBroadcastReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            init();
        }
    };
    BroadcastReceiver nichengBroadcast = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            init();
        }
    };

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            case R.id.person_img:
            case R.id.my_xinxi:
            case R.id.person_imgtwo:
                if (IsStringSPUtils.IsUid() == null || IsStringSPUtils.IsUid().equals("")) {
                    LoginController.setLoginState(new UnLoginState());
                }
                LoginController.PersonnalInfomaton(getActivity(),null);
                break;
            case R.id.myBooks:
                //微测试
                if (IsStringSPUtils.IsUid() == null || IsStringSPUtils.IsUid().equals("")) {
                    intent = new Intent(getActivity(), LoginActivity.class);
                    startActivity(intent);
                } else {
                    intent = new Intent(getActivity(), ActivityBooks.class);
                    startActivity(intent);
                }
                break;
            case R.id.my_zhanghu:
                if (IsStringSPUtils.IsUid() == null || IsStringSPUtils.IsUid().equals("")) {
                    LoginController.setLoginState(new UnLoginState());
                }
                LoginController.myZhangHu(getActivity(),null);
                break;
            case R.id.my_anquan:
                if (IsStringSPUtils.IsUid() == null || IsStringSPUtils.IsUid().equals("")) {
                    LoginController.setLoginState(new UnLoginState());
                }
                LoginController.anQuan(getActivity(),null);
                break;
            case R.id.tuijian:
                intent = new Intent(getActivity(), WebActivity.class);
                intent.putExtra("title", "推荐好友");
                intent.putExtra("url", Const.WEB + "web/recommend?uid="+(String) SPUtils.get(MyApplication.instance, null, Const.ID, ""));
                startActivity(intent);
                break;
            case R.id.my_bangzhu:
                intent = new Intent(getActivity(), WebActivity.class);
                intent.putExtra("title", "帮助中心");
                intent.putExtra("url", Const.WEB + "web/helper");
                startActivity(intent);
                break;
            case R.id.my_ok:
//                onPlayer();
                exit();
                // 发送广播通知LoginActivity结束
                Intent intent1 = new Intent(MeFragment.EXIT);
                getActivity().sendBroadcast(intent1);
                break;
            case R.id.img_setting:
                if (IsStringSPUtils.IsUid() == null || IsStringSPUtils.IsUid().equals("")) {
                    intent = new Intent(getActivity(), LoginActivity.class);
                    startActivity(intent);
                } else {
                    intent = new Intent(getActivity(), SetUpActivity.class);
                    startActivity(intent);
                }
                break;
        }
    }

    private void exit() {
        // TODO Auto-generated method stub
        SPUtils.put(getActivity(), null, Const.USERNAME, "");
        MyApplication.getInstance().mPushAgent.removeAlias(
                SPUtils.get(getActivity(), null, Const.ID, "") + "", Const.UMENG_ALIAS,
                new UTrack.ICallBack() {
                    @Override
                    public void onMessage(boolean arg0, String arg1) {
                        // TODO Auto-generated method stub
                        System.out.println("删除别名" + arg0 + "arg1" + arg1);
                    }
                });
//		SPUtils.put(MainApplication.context, null, SpPublicParams.USERHEAD, "");
        my_ok.setVisibility(View.GONE);

        SPUtils.put(getActivity(), null,
                Const.ID, "");
        SPUtils.put(getActivity(), null,
                Const.USERNAME, "");
        SPUtils.put(getActivity(), null,
                Const.PASSWORD, "");
        SPUtils.put(getActivity(), null,
                Const.MOBILE, "");
        SPUtils.put(getActivity(), null,
                Const.IS_LOGINED, false);
        SPUtils.put(getActivity(), null,
                Const.HEAD,"");
        SPUtils.put(getActivity(), null,
                Const.NICK, "");
        SPUtils.put(getActivity(), null,
                Const.EMAIL, "");
        SPUtils.put(getActivity(), null,
                Const.READE_NAME, "");
        SPUtils.put(getActivity(), null,
                Const.Andress, "");
        SPUtils.put(getActivity(), null,
                Const.S_ID, "");
        SPUtils.put(getActivity(), null,
                Const.SEX, "");
        SPUtils.put(getActivity(), null,
                Const.AUTO_CODE, "");
        SPUtils.put(getActivity(), null,
                Const.SCHOOL_NAME, "");
        LoginController.setLoginState(new UnLoginState());
        init();
    }

    private void inittwo() {

//        actionBar = (TranslucentActionBar)getActivity(). findViewById(R.id.actionbar);
//        //初始actionBar
//        actionBar.setData("个人中心", 0, null, 0, null, null);
//        //开启渐变
//        actionBar.setNeedTranslucent();
        //设置状态栏高度

//        actionBar.setStatusBarHeight(getStatusBarHeight());

        translucentScrollView = (TranslucentScrollView) getActivity().findViewById(R.id.pullzoom_scrollview);
        //设置透明度变化监听
        translucentScrollView.setTranslucentChangedListener(this);
        //关联需要渐变的视图
//        translucentScrollView.setTransView(actionBar);

        zoomView = getActivity().findViewById(R.id.my_toolbar);
        //关联伸缩的视图
        translucentScrollView.setPullZoomView(zoomView);
    }


    @Override
    public void onLeftClick() {

    }

    @Override
    public void onRightClick() {

    }

    @Override
    public void onTranslucentChanged(int transAlpha) {
//        actionBar.tvTitle.setVisibility(transAlpha > 48 ? View.VISIBLE : View.GONE);
    }
}
